#!	
# a simple program for shuffle the cards
# demostrate how to use complex data structures in perl 
# by jzhang @ 17:24 2004-01-12
#

use warnings;
use strict;
use List::Util qw(shuffle); 

#guard for output shuffle and arrange cards process...
my $write_process = 0;

my @numbers = (0,'A',1,2,3,4,5,6,7,8,9,10,'J','Q','K');
my %cards; #hash of arrays
my ($person, $group, $number) = (0,0,0);
my %game;  # hash of ( hash of array )

srand;

#shuffle the cards: 
for $group qw(spade hearts clubs diamonds)
{
    $cards{$group} = [ shuffle(1..13) ];
    print "$group: ", join(" ",@{$cards{$group}} ),"\n" if $write_process;
}

# arrange cards to person
for (1..52/4)
{
    print $_," " if $write_process;
    for $person qw(Rose Mary John Peter)
    {
        $group = (keys %cards)[int rand(keys %cards)];
        $number = shift @{ $cards{$group} };
        #if no more card in this group, delete this group
        delete $cards{$group} if @{ $cards{$group} } == 0;

        push @{ $game{$person}{$group} }, $numbers[$number];
        print "($person,$group,$number)" if $write_process;
    }
     print "\n" if $write_process;
}

#print the arrangement
foreach $person (keys %game)
{
    print "$person: \n";
    foreach $group ( keys %{ $game{$person} } )
    {
        print "\t($group ", join(" ",@{ $game{$person}{$group} } ),")\n";
    }
}
